package com.lkd.controller;


import com.lkd.dto.SkuDto;
import com.lkd.entity.Sku;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.SkuService;
import com.lkd.utils.AliOssUtil;
import com.lkd.vo.SkuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
public class SkuController extends Result{

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private SkuService skuService;

    //图片上传
    @PostMapping("/api/vm-service/sku/fileUpload")
    public Result upload(MultipartFile fileName) throws IOException {
        String filePath = aliOssUtil.upload(fileName.getOriginalFilename(), fileName.getInputStream());
        log.info("文件上传后的地址:{}", filePath);
        return success(filePath);
    }

    //分页查询
    @GetMapping("/api/vm-service/sku/search")
    public Result findByPage(@RequestParam(name = "pageIndex",defaultValue = "1") Integer pageIndex,
                             @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(name = "skuName",required = false) String skuName){
        PageBean<SkuVo> pageBean = skuService.findByPage(pageIndex,pageSize,skuName);
        return success(pageBean);
    }

    //新增商品
    @PostMapping("/api/vm-service/sku")
    public Result addSku(@RequestBody Sku sku){
         skuService.addSku(sku);
         return success();
    }

    //修改商品
    @PutMapping("/api/vm-service/sku")
    public Result updateSku(@RequestBody Sku sku){
        skuService.updateSku(sku);
        return success();
    }
}
